package com.whjfxx.terminal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whjfxx.terminal.base.BaseController;
import com.whjfxx.terminal.biz.param.SearchParam;
import com.whjfxx.terminal.biz.param.TerminalPageParam;
import com.whjfxx.terminal.biz.param.TerminalParam;
import com.whjfxx.terminal.biz.service.PoImportLogService;
import com.whjfxx.terminal.biz.service.PoTerminalService;
import com.whjfxx.terminal.biz.vo.ImportDataVo;
import com.whjfxx.terminal.biz.vo.TerminalVo;
import com.whjfxx.terminal.commons.IdsParam;
import com.whjfxx.terminal.commons.Result;
import com.whjfxx.terminal.exceptions.BizException;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * Created by b.c on 2022/6/7 16:30
 */
@RestController
@Log4j2
@RequestMapping("terminal")
public class PoTerminalController extends BaseController {

    @Autowired
    PoTerminalService poTerminalService;

    @Autowired
    PoImportLogService poImportLogService;

    @GetMapping("page")
    public Result<Page<TerminalVo>> pageList(SearchParam<TerminalPageParam> param) {
        Page<TerminalVo> page = poTerminalService.pageList(param);
        return Result.success(page);
    }

    @PostMapping("save")
    public Result<Object> save(@Validated TerminalParam param) {
        if (Objects.isNull(param.getId())) {
            poTerminalService.insertSave(param, null);
        } else {
            poTerminalService.updateSave(param);
        }
        return Result.success();
    }


    @PostMapping("delete")
    public Result<Object> delete(@Validated @RequestParam("id") Long id) {
        poTerminalService.delete(id);
        return Result.success();
    }

    @PostMapping("disable")
    public Result<Object> disable(@Validated IdsParam param) {
        if (CollectionUtils.isEmpty(param.getIds())) {
            throw BizException.build("参数不能为空");
        }
        poTerminalService.disable(param.getIds());
        return Result.success();
    }

    @PostMapping("enable")
    public Result<Object> enable(@Validated IdsParam param) {
        if (CollectionUtils.isEmpty(param.getIds())) {
            throw BizException.build("参数不能为空");
        }
        poTerminalService.enable(param.getIds());
        return Result.success();
    }

    @PostMapping("upload")
    public Result<String> upload(@RequestParam("file") MultipartFile multipartFile) {
        try {
            XSSFWorkbook workbook = new XSSFWorkbook(multipartFile.getInputStream());
            String uuid = poImportLogService.importXlsx(multipartFile, workbook);
            return Result.success(uuid);
        } catch (IOException e) {
            e.printStackTrace();
            throw BizException.build("读取文件失败");
        }
    }

    @GetMapping("readData")
    public Result<List<ImportDataVo>> readData(@RequestParam(value = "batchNo", required = false) String batchNo) {
        if (StringUtils.isBlank(batchNo)) {
            return Result.fail("参数错误");
        }
        List<ImportDataVo> vos = this.poImportLogService.readXlsx(batchNo);
        return Result.success(vos);
    }

    @PostMapping("import")
    public Result<String> importData(@RequestParam(value = "batchNo", required = false) String batchNo) {
        if (StringUtils.isBlank(batchNo)) {
            return Result.fail("参数错误");
        }
        // 校验数据
        this.poImportLogService.valid(batchNo);
        this.poTerminalService.import2Terminal(batchNo);
        return Result.success();
    }
}
